package cn.bdqn.controller;

import cn.bdqn.pojo.BasDict;
import cn.bdqn.pojo.CstService;
import cn.bdqn.pojo.Customer;
import cn.bdqn.pojo.User;
import cn.bdqn.service.BasDictService;
import cn.bdqn.service.CstServiceService;
import cn.bdqn.service.CustomerService;
import cn.bdqn.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/service")
public class CstServiceController {
    @Resource
    CustomerService customerService;
    @Resource
    CstServiceService cstServiceService;
    @Resource
    BasDictService basDictService;
    @Resource
    UserService userService;
    /**
     * 跳转服务新增页面
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model){
        List<Customer> allCustomers = customerService.findAllCustomers();
        model.addAttribute("customerList",allCustomers);

        List<BasDict> basDicts = basDictService.getDictByDictType("服务类型");
        model.addAttribute("typeList",basDicts);
        return  "service/add";
    }

    /**
     * 保存
     * @param cstService
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/save")
    public String save(CstService cstService, HttpSession session,Model model){
        //创建人id
        User loginUser = (User)session.getAttribute("loginUser");
        cstService.setSvrCreateId(loginUser.getUsrId());
        //创建人名称
        cstService.setSvrCreateBy(loginUser.getUsrName());
        try {
            cstServiceService.addCstService(cstService);
            model.addAttribute("msg","新增成功");
            return "redirect:/service/add";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg","新增失败");
            return "redirect:/service/add";
        }
    }

    /**
     * 服务分配列表
     * @param custName
     * @param title
     * @param type
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/dispatch")
    public String dispatch(String custName,String title,String type, Model model,
                           @RequestParam(defaultValue = "1")Integer pageIndex){
        //设置每页显示条数
        int pageSize = 3;
        //查询每页显示的数据
        IPage<CstService> cstServicePager=cstServiceService.
                getCstServicesByPage(custName,title,type,"新创建",pageIndex,pageSize);
        model.addAttribute("cstServicePager", cstServicePager);
        //查询条件回显
        model.addAttribute("custName",custName);
        model.addAttribute("title",title);
        model.addAttribute("type",type);
        //查询服务类型
        List<BasDict> typeList=basDictService.getDictByDictType("服务类型");
        model.addAttribute("typeList",typeList);
        //查询所有角色为“客户经理”的用户
        List<User> userList=userService.getUsersByRoleId(2L);
        model.addAttribute("userList",userList);
        return "service/dispatch_list";
    }

    /**
     * 保存服务分配信息
     * @param cstService
     * @param session
     * @return
     */
    @PostMapping("/dispatch")
    @ResponseBody
    public Map<String, Object> dispatch(CstService cstService, HttpSession session)
    {
        Map<String, Object> map = new HashMap<>();
    //设置分配时间为当前时间
        cstService.setSvrDueDate(new Date());
    //设置服务状态为已分配
        cstService.setSvrStatus("已分配");
        try {
            cstServiceService.updateCstService(cstService);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }
    /**
     * 删除服务
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            cstServiceService.deleteCstService(id);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }
    /**
     * 服务处理列表页面
     * @param custName
     * @param title
     * @param type
     * @param model
     * @param pageIndex
     * @return
     */
    @GetMapping("/deal")
    public String deal(String custName,String title,String type, Model model,
                       @RequestParam(defaultValue = "1")Integer pageIndex){
        //设置每页显示条数
        int pageSize = 3;
        //查询每页显示的数据
        IPage<CstService> cstServicePager=cstServiceService.getCstServicesByPage(custName,title,type,"已分配",pageIndex,pageSize);
        model.addAttribute("cstServicePager", cstServicePager);
        //查询条件回显
        model.addAttribute("custName",custName);
        model.addAttribute("title",title);
        model.addAttribute("type",type);
        //查询服务类型
        List<BasDict> typeList=basDictService.getDictByDictType("服务类型");
        model.addAttribute("typeList",typeList);
        return "service/deal_list";
    }

    /**
     * 跳转服务处理页面
     * @param svrId
     * @param model
     * @return
     */
    @GetMapping("/toDeal")
    public String toDeal(Long svrId,Model model){
        CstService cstService=cstServiceService.getCstService(svrId);
        model.addAttribute("cstService",cstService);
        return "service/deal";
    }
    /**
     * 保存服务处理信息
     * @param cstService
     * @param session
     * @return
     */
    @RequestMapping("/deal")
    public String deal(CstService cstService, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //获取当前登录人
        User loginUser=(User)session.getAttribute("loginUser");
        //设置处理人id
        cstService.setSvrDealId(loginUser.getUsrId());
        //设置处理人姓名
        cstService.setSvrDealBy(loginUser.getUsrName());
        //设置处理时间为当前时间
        cstService.setSvrDealDate(new Date());
        //设置服务状态为已处理
        cstService.setSvrStatus("已处理");
        try {
            cstServiceService.updateCstService(cstService);;
            return "redirect:/service/deal";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/service/toDeal";
        }
    }

    /**
     * 服务反馈列表页面
     * @param custName
     * @param title
     * @param type
     * @param model
     * @param pageIndex
     * @return
     */
    @GetMapping("/feedback")
    public String feedback(String custName,String title,String type, Model model,
                           @RequestParam(defaultValue = "1")Integer pageIndex) {
        //设置每页显示条数
        int pageSize = 3;
        //查询每页显示的数据
        IPage<CstService> cstServicePager = cstServiceService.
                getCstServicesByPage(custName, title, type, "已处理", pageIndex, pageSize);
        model.addAttribute("cstServicePager", cstServicePager);
        //查询条件回显
        model.addAttribute("custName", custName);
        model.addAttribute("title", title);
        model.addAttribute("type", type);
        //查询服务类型
        List<BasDict> typeList = basDictService.getDictByDictType("服务类型");
        model.addAttribute("typeList", typeList);
        return "service/feedback_list";
    }
    /**
     * 跳转服务反馈页面
     * @param svrId
     * @param model
     * @return
     */
    @GetMapping("/toFeedback")
    public String toFeedback(Long svrId,Model model) {
        CstService cstService = cstServiceService.getCstService(svrId);
        model.addAttribute("cstService", cstService);
        return "service/feedback";
    }
    /**
     * 保存服务反馈信息
     * @param cstService
     * @param session
     * @return
     */
    @PostMapping("/feedback")
    public String feedback(CstService cstService, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //获取当前登录人
        User loginUser=(User)session.getAttribute("loginUser");
        //设置处理人id
        cstService.setSvrDealId(loginUser.getUsrId());
        //设置处理人姓名
        cstService.setSvrDealBy(loginUser.getUsrName());
        //设置处理时间为当前时间
        cstService.setSvrDealDate(new Date());
        if(cstService.getSvrSatisfy()>=3){
        //设置服务状态为已归档
            cstService.setSvrStatus("已归档");
        }else{
        //设置服务状态为已分配
            cstService.setSvrStatus("已分配");
        }
        try {
            cstServiceService.updateCstService(cstService);;
            return "redirect:/service/feedback";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/service/toFeedback";
        }
    }

    /**
     * 服务归档列表页面
     * @param custName
     * @param title
     * @param type
     * @param model
     * @param pageIndex
     * @return
     */
    @GetMapping("/arch")
    public String arch(String custName,String title,String type, Model model,
                       @RequestParam(defaultValue = "1")Integer pageIndex){
        //设置每页显示条数
        int pageSize = 3;
        //查询每页显示的数据
        IPage<CstService> cstServicePager=cstServiceService.
                getCstServicesByPage(custName,title,type,"已归档",pageIndex,pageSize);
        model.addAttribute("cstServicePager", cstServicePager);
        //查询条件回显
        model.addAttribute("custName",custName);
        model.addAttribute("title",title);
        model.addAttribute("type",type);
        //查询服务类型
        List<BasDict> typeList=basDictService.getDictByDictType("服务类型");
        model.addAttribute("typeList",typeList);
        return "service/arch_list";
    }
    /**
     * 跳转归档服务查看页面
     * @param svrId
     * @param model
     * @return
     */
    @GetMapping("/view")
    public String view(Long svrId, Model model){
        CstService cstService=cstServiceService.getCstService(svrId);
        model.addAttribute("cstService",cstService);
        return "service/view";
    }
}
